{smcl}
{* 07jul2006}{...}
{cmd:help mata mm_locate()}
{hline}

{title:Title}

{p 4 19 2}
{bf:mm_locate() -- Search an ordered vector}


{title:Syntax}

{p 8 21 2}{bind:  }{it:void}{bind:  }{cmd:mm_locate(}{it:y}{cmd:,}
{it:x}{cmd:,} {it:j}{cmd:)}

{p 8 21 2}{bind:  }{it:void}{bind:  }{cmd:mm_hunt(}{it:y}{cmd:,}
{it:x}{cmd:,} {it:j}{cmd:)}

{pstd}
where

{p 12 16 2}
{it:y}:  {it:numeric vector} containing data; {it:y} must be ordered,
either ascending or descending

{p 12 16 2}
{it:x}:  {it:numeric scalar} containing value to be located in {it:y}

{p 12 16 2}
{it:j}:  {it:real scalar} containing initial guess for {cmd:mm_hunt()};
will be replaced by a {it:real scalar} such that {it:x} lies
between {it:y}[{it:j}] and {it:y}[{it:j}+1]


{title:Description}

{pstd} Given an ordered data vector {it:y} and some value {it:x},
{cmd:mm_locate()}, replaces {it:j} such that {it:x} lies between
{it:y}[{it:j}] and {it:y}[{it:j}+1]. {it:j}==0 or {it:j}==length({it:y}) is returned,
if {it:x} is out of range. {cmd:mm_locate()} applies the bisection
method to locate {it:x} in {it:y}.

{pstd}{cmd:mm_hunt()} also locates {it:x} in {it:y}. However,
{cmd:mm_hunt()} uses the input value of {it:j} as the starting point
for the search process and applies a modified algorithm.
{cmd:mm_hunt()} is useful if consecutive searches be performed for
similar values.

{pstd}
{cmd:mm_locate()}
is a translation of the {cmd:locate} algorithm in Press et
al. (1992:117). {cmd:mm_hunt()}
is a translation of the {cmd:hunt} algorithm in Press et
al. (1992:118-119).


{title:Remarks}

{pstd}None.


{title:Conformability}

    {cmd:mm_locate(}{it:y}{cmd:,} {it:x}{cmd:,} {it:j}{cmd:)},
    {cmd:mm_hunt(}{it:y}{cmd:,} {it:x}{cmd:,} {it:j}{cmd:)}:
    {it:input:}
           {it:y}:  {it:n x} 1 or 1 {it:x n}
           {it:x}:  1 {it:x} 1
           {it:j}:  1 {it:x} 1
    {it:output:}
           {it:j}:  1 {it:x} 1


{title:Diagnostics}

{pstd}None.


{title:Source code}

{pstd}
{help moremata_source##mm_locate:mm_locate.mata}


{title:References}

{phang}
Press, William H.,
Saul A. Teukolsky,
William T. Vetterling,
Brian P. Flannery (1992). Numerical Recipes in C. The Art of Scientific
Computing. Second Edition. Cambridge University Press.
{browse "http://www.numerical-recipes.com/"}


{title:Author}

{pstd} Ben Jann, University of Bern, jann@soz.unibe.ch


{title:Also see}

{psee}
Online:  help for
{bf:{help m4_utility:[M-4] utility}},
{bf:{help moremata}}
